/*****************************************************************************
	SNEGA UTILITIES 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/snega-utilities/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package org.sng.util.web.elmt;

import javax.servlet.http.*;

import org.sng.util.var.SString;
import org.sng.util.var.SVoid;

public class NodeSelect extends Node {
	
	public NodeSelect()
	{
		super();
	}
	
	public NodeSelect(int n)
	{
		super(n);
	}
	
	public NodeSelect(String id)
	{
		super();
		
		this.PushProperty("id", id);
	}
	
	public NodeSelect(int n, String id)
	{
		super(n);
		
		this.PushProperty("id", id);
	}
	
	public NodeSelect Create()
	{
		return new NodeSelect();
	}
	
	public NodeSelect Create(int n)
	{
		return new NodeSelect(n);
	}
	
	public NodeSelect Create(String id, RuntimeEnvironment re)
	{
		HttpServletRequest request = re.GetRequest();
		
		NodeSelect o = new NodeSelect();
		
		o.m_properties.put("id", new SString(id));
		
		o.m_properties.put("value", new SString(request.getParameter(id + "_value")));
		
		String postback = request.getParameter(id + "_postback");
		if (postback != null)
			o.PushProperty("postback", postback);
		
		return o;
	}
	
	public String GetType()
	{
		return "select";
	}
	
	public StringBuffer Do(RuntimeEnvironment re) throws Exception
	{
		StringBuffer result = new StringBuffer();
		
		SVoid prop = null;
		
		if ((prop = this.m_properties.get("id")) == null)
		{
			throw new Exception("Critical property \"id\" is missing.");
		}	
		String sid = prop.GetString(re);
		
		if ((prop = this.m_properties.get("monitor")) != null)
		{
			String [] moniter = prop.GetString(re).split(";");
			
			for (int i = 0; i < moniter.length; i++) {
				if (moniter[i].compareTo("onChanged") == 0)
				{
					// Moniter on select changed
					re.AppendJavascript("$(\"" + sid + "\").onchange=function(){var para=GetPostList();process(g_processUrl+\"?id=" + sid + "&event=Changed\",para,null,null);};");
				}
			}
		}
		
		StringBuffer properties = new StringBuffer();
		if ((prop = this.m_properties.get("class")) != null) {
			properties.append(" class=\"");
			properties.append(prop.GetString(re));
			properties.append("\"");
		}
		if ((prop = this.m_properties.get("style")) != null) {
			properties.append(" style=\"");
			properties.append(prop.GetString(re));
			properties.append("\"");
		}
		
		result.append("<select id=\"" + sid + "\" name=\"" + sid + "\" " + properties + ">");
		result.append("</select>");
		
		Node [] options = this.GetNodeArrayByType("option");
		for (int i = 0; options != null && i < options.length; i++) {
			re.AppendJavascript("addLineToSelect($(\"" + sid + "\"), \"" + ((NodeOption)options[i]).GetKey(re) + "\", \"" + ((NodeOption)options[i]).GetValue(re) + "\");");
		}
		
		return result;
	}
	
	public void AddOption(RuntimeEnvironment re, String key, String value)
	{
		SVoid prop = null;
		if ((prop = this.m_properties.get("id")) == null)
		{
			return;
		}	
		String sid = prop.GetString(re);
		
		re.AppendJavascript("addLineToSelect($(\"" + sid + "\"), \"" + key + "\", \"" + value + "\");");
	}
	
	public void ClearOptions(RuntimeEnvironment re)
	{
		SVoid prop = null;
		if ((prop = this.m_properties.get("id")) == null)
		{
			return;
		}	
		String sid = prop.GetString(re);
		
		re.AppendJavascript("while($(\"" + sid + "\").options.length!=0){$(\"" + sid + "\").remove(0);};");
	}
	
	public void AppendPostList(RuntimeEnvironment re) {
		String sid = this.m_properties.get("id").GetString(re);
		
		re.AppendPostList("if($(\"" + sid + "\")!=null){");
		re.AppendPostList("para.add(\"cid\", \"" + sid + "\");");
		re.AppendPostList("para.add(\"type\", \"Select\");");
		re.AppendPostList("para.add(\"" + sid + "_value\", $(\"" + sid + "\").value);");
		
		SVoid postback = this.GetProperty("postback");
		if (postback != null)
			re.AppendPostList(sid + "_postback", "\"" + SString.ToStringLiterealSafe(postback.GetString(re)) + "\"");
		
		re.AppendPostList("};");
	}
	
	public SVoid GetCurrentValue(RuntimeEnvironment re)
	{
		return this.GetProperty("value");
	}

}
